
    (function (xhr) {

        var XHR = XMLHttpRequest.prototype;

        var open = XHR.open;
        var send = XHR.send;

        let interceptList = [
            // {
            //     url:'/getDouyinReverseNotify',eventName:'findDouyinOrder'
            // },
            {
                url:'/getDispatchOrderCount',eventName: 'dispatchOrderCount'
        }] // 需要拦截的请求路径

        XHR.open = function (method, url) {
            this._method = method;
            this._url = url;
            return open.apply(this, arguments);
        };

        XHR.send = function (postData) {
            // console.log('xhr request:', this._method, this._url, postData);
            let method = this._method
            let url = this._url
            // 适合的请求拦截 并发送给content.js
            let intercept = interceptList.find(intercep => url.includes(intercep.url))
            // console.log('intercept',intercept)
            if (intercept) {
                // 符合拦截情况的请求
                // console.log('符合拦截列表的XHR请求 xhr request:', method, url, postData);
                this.addEventListener('load', function () {
                    // sessionStorage['key'] = JSON.stringify(response); // 插件需要添加'storage'权限
                    // document.cookie
                    // localStorage['key']
                    // 将url和method传递给content.js然后进行判断是否是要进行相应的逻辑
                    window.postMessage({ type: 'xhr', data: this.response,url,method,eventName:intercept.eventName }, '*');  // 将响应发送到 content script
                });
            }
            return send.apply(this, arguments);
        };
    })(XMLHttpRequest);

(function () {
    let origFetch = window.fetch;
    window.fetch = async function (...args) {
        const response = await origFetch(...args);
        console.log('fetch request:', args);

        response
            .clone()
            .blob() // 此处需要根据不同数据调用不同方法，这里演示的是二进制大文件，比如音频
            .then(data => {
                // 对于二进制大文件可以创建为URL(blob:开头)，供其它脚本访问
                //sessionStorage['wave'] = URL.createObjectURL(data); // 插件需要添加'storage'权限
                window.postMessage({ type: 'fetch', data: URL.createObjectURL(data) }, '*'); // send to content script
            })
            .catch(err => console.error(err));
        return response;
    }
})();
